package com.group12.liuxinpeng.role.service.impl;

import com.group12.liuxinpeng.mybatis.entity.PtRoleEntity;
import com.group12.liuxinpeng.mybatis.flexigrid.PadingRstType;
import com.group12.liuxinpeng.mybatis.flexigrid.PagingBean;
import com.group12.liuxinpeng.mybatis.ztree.ZtreeEntity;
import com.group12.liuxinpeng.role.entity.SearchRoleEntity;
import com.group12.liuxinpeng.role.repository.RoleDao;
import com.group12.liuxinpeng.role.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName RoleServiceImpl
 * @Description: //TODO
 * @Author wyq
 * @Date 2022/5/16 15:36
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    @Override
    public PadingRstType<PtRoleEntity> queryRoleListPage(SearchRoleEntity search, PagingBean paging) {
        paging.deal(PtRoleEntity.class);
        PadingRstType<PtRoleEntity> ptRoleEntityPadingRstType = new PadingRstType<PtRoleEntity>();
        ptRoleEntityPadingRstType.setPage(paging.getPage());
        List<PtRoleEntity> list = roleDao.queryRoleListPage(search,paging);
        ptRoleEntityPadingRstType.setRawRecords(list);
        Integer total = roleDao.queryRoleTotal(search);
        ptRoleEntityPadingRstType.setTotal(total);
        ptRoleEntityPadingRstType.putItems();
        return ptRoleEntityPadingRstType;

    }

    @Override
    public List<ZtreeEntity> queryMenuByRoleId(String roleUuid) {
        List<ZtreeEntity> list = roleDao.queryMenuTree();
        List<String> menuIdList = roleDao.queryMenuIdByRoleId(roleUuid);
        int index = 0;
        for(ZtreeEntity item: list){
            if(index > 10 ){
                item.setOpen(false);
            }
            index++;
            for(String menuId : menuIdList){
                if(menuId.equals(item.getId())){
                    item.setChecked(true);
                }
            }
        }
        return list;
    }

    @Override
    public void saveMenuIdAndRoleId(String roleUuid, String menuArray) {
        roleDao.deleteMenuByRoleId(roleUuid);
        String[] menuIds = menuArray.split(",");
        for(String menuId: menuIds){
            roleDao.insertMenuAndRole(roleUuid,menuId);
        }
    }

}
